package com.baiyun.lease.web.admin.service.impl;

import com.baiyun.lease.common.exception.LeaseException;
import com.baiyun.lease.common.result.Result;
import com.baiyun.lease.common.result.ResultCodeEnum;
import com.baiyun.lease.model.entity.*;
import com.baiyun.lease.model.enums.ItemType;
import com.baiyun.lease.web.admin.mapper.*;
import com.baiyun.lease.web.admin.service.*;
import com.baiyun.lease.web.admin.vo.apartment.ApartmentDetailVo;
import com.baiyun.lease.web.admin.vo.apartment.ApartmentItemVo;
import com.baiyun.lease.web.admin.vo.apartment.ApartmentQueryVo;
import com.baiyun.lease.web.admin.vo.apartment.ApartmentSubmitVo;
import com.baiyun.lease.web.admin.vo.fee.FeeValueVo;
import com.baiyun.lease.web.admin.vo.graph.GraphVo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author liubo
 * @description 针对表【apartment_info(公寓信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
public class ApartmentInfoServiceImpl extends ServiceImpl<ApartmentInfoMapper, ApartmentInfo>
        implements ApartmentInfoService {
    @Autowired
    private GraphInfoService graphInfoService;
    @Autowired
    private ApartmentFacilityService apartmentFacilityService;
    @Autowired
    private ApartmentLabelService apartmentLabelService;
    @Autowired
    private ApartmentFeeValueService apartmentFeeValueService;
    @Autowired
    private  ApartmentInfoMapper apartmentInfoMapper;
    @Autowired
    private GraphInfoMapper graphInfoMapper;
    @Autowired
    private LabelInfoMapper labelInfoMapper;
//    @Autowired
//    private FacilityInfoMapper facilityInfoMapper;
    @Autowired
    private FeeValueMapper feeValueMapper;
    @Autowired
    private RoomInfoService roomInfoService;

    @Override
    public void saveOrUpdateApartment(ApartmentSubmitVo apartmentSubmitVo) {
        boolean bo = apartmentSubmitVo.getId() != null;
        this.saveOrUpdate(apartmentSubmitVo);
        //若有id即为更新操作，先删除数据库中原有的数据再进行重新保存
        if (bo){
            //1.删除图片列表
            graphInfoService.lambdaUpdate()
                    .eq(GraphInfo::getItemType, ItemType.APARTMENT)
                    .eq(GraphInfo::getItemId,apartmentSubmitVo.getId())
                    .remove();
            //2.删除公寓配套列表
            apartmentFacilityService.lambdaUpdate()
                    .eq(ApartmentFacility::getApartmentId,apartmentSubmitVo.getId())
                    .remove();
            //3.删除公寓标签列表
            apartmentLabelService.lambdaUpdate()
                    .eq(ApartmentLabel::getApartmentId,apartmentSubmitVo.getId())
                    .remove();
            //4.删除公寓杂费列表
            apartmentFeeValueService.lambdaUpdate()
                    .eq(ApartmentFeeValue::getApartmentId,apartmentSubmitVo.getId())
                    .remove();
        }
        //5.添加图片列表
        List<GraphVo> graphVoList = apartmentSubmitVo.getGraphVoList();
        if (!CollectionUtils.isEmpty(graphVoList)){
            ArrayList<GraphInfo> graphInfoList = new ArrayList<>();
            for (GraphVo graphVo : graphVoList) {
                GraphInfo graphInfo = new GraphInfo();
                graphInfo.setItemType(ItemType.APARTMENT);
                graphInfo.setItemId(apartmentSubmitVo.getId());
                graphInfo.setName(graphVo.getName());
                graphInfo.setUrl(graphVo.getUrl());
                graphInfoList.add(graphInfo);
            }
            graphInfoService.saveBatch(graphInfoList);
        }
        //6.添加公寓的配套列表
        List<Long> facilityInfoIdList = apartmentSubmitVo.getFacilityInfoIds();
        if (!CollectionUtils.isEmpty(facilityInfoIdList)){
            ArrayList<ApartmentFacility> facilityList = new ArrayList<>();
            for (Long facilityId : facilityInfoIdList) {
                ApartmentFacility apartmentFacility = new ApartmentFacility();
                apartmentFacility.setApartmentId(apartmentSubmitVo.getId());
                apartmentFacility.setFacilityId(facilityId);
                facilityList.add(apartmentFacility);
            }
            apartmentFacilityService.saveBatch(facilityList);
        }
        //7.添加公寓标签列表
        List<Long> labelIds = apartmentSubmitVo.getLabelIds();
        if (!CollectionUtils.isEmpty(labelIds)){
            ArrayList<ApartmentLabel> apartmentLabels = new ArrayList<>();
            for (Long labelId : labelIds) {
                ApartmentLabel apartmentLabel = new ApartmentLabel();
                apartmentLabel.setApartmentId(apartmentSubmitVo.getId());
                apartmentLabel.setLabelId(labelId);
                apartmentLabels.add(apartmentLabel);
            }
            apartmentLabelService.saveBatch(apartmentLabels);
        }
        //8.添加杂费列表
        List<Long> feeValueIds = apartmentSubmitVo.getFeeValueIds();
        if (!CollectionUtils.isEmpty(feeValueIds)) {
            ArrayList<ApartmentFeeValue> apartmentFeeValueList = new ArrayList<>();
            for (Long feeValueId : feeValueIds) {
                ApartmentFeeValue apartmentFeeValue = new ApartmentFeeValue();
                apartmentFeeValue.setApartmentId(apartmentSubmitVo.getId());
                apartmentFeeValue.setFeeValueId(feeValueId);
                apartmentFeeValueList.add(apartmentFeeValue);
            }
            apartmentFeeValueService.saveBatch(apartmentFeeValueList);
        }
    }

    @Override
    public IPage<ApartmentItemVo> pageItem(long current, long size, ApartmentQueryVo queryVo) {
        Page<ApartmentItemVo> page = new Page<>(current, size);
        IPage<ApartmentItemVo> pageApartmentItemVo = apartmentInfoMapper.pageItem(page,queryVo);
        return pageApartmentItemVo;
    }

    @Override
    public ApartmentDetailVo getDetailById(Long id) {
        //1.查询公寓信息
        ApartmentInfo apartmentInfo = this.getById(id);
        if (apartmentInfo == null) {
            return null;
        }
        //2.查询图片列表GraphInfo
        List<GraphVo> graphVoList = graphInfoMapper.selectListByItemTypeAndId(ItemType.APARTMENT, id);
        //3.查询标签列表LabelInfo
        List<LabelInfo> labelInfoList = labelInfoMapper.selectListByApartmentId(id);
//        //4.查询配套列表FacilityInfo
//        List<FacilityInfo> facilityInfoList = facilityInfoMapper.selectListByApartmentId(id);
        //5.查询杂费列表FeeValue
        List<FeeValueVo> feeValueVoList = feeValueMapper.selectListByApartmentId(id);

        ApartmentDetailVo adminApartmentDetailVo = new ApartmentDetailVo();

        //属性拷贝 将一个apartmentInfo对象的属性值复制到另一个apartmentInfo对象中对应的属性上
        BeanUtils.copyProperties(apartmentInfo, adminApartmentDetailVo);

        adminApartmentDetailVo.setGraphVoList(graphVoList);
        adminApartmentDetailVo.setLabelInfoList(labelInfoList);
//        adminApartmentDetailVo.setFacilityInfoList(facilityInfoList);
        adminApartmentDetailVo.setFeeValueVoList(feeValueVoList);

        return adminApartmentDetailVo;

    }

    @Override
    public Result removeApartmentById(Long id) {
        //先统计公寓是否有房间
        Long count = roomInfoService.lambdaQuery()
                .eq(RoomInfo::getApartmentId, id)
                .count();
        if (count > 0) {
            throw new LeaseException(ResultCodeEnum.ADMIN_APARTMENT_DELETE_ERROR);
            //return Result.build(count, ResultCodeEnum.DELETE_ERROR);
        }
        //1.删除公寓基本信息
        this.removeById(id);
        //2.删除公寓图片信息
        graphInfoService.lambdaUpdate()
                .eq(GraphInfo::getItemType,ItemType.APARTMENT)
                .eq(GraphInfo::getItemId,id)
                .remove();
        //3.删除配套关系表
        apartmentFacilityService.lambdaUpdate()
                .eq(ApartmentFacility::getApartmentId,id)
                .remove();
        //4.删除标签关系 ApartmentLabel
        apartmentLabelService.lambdaUpdate()
                .eq(ApartmentLabel::getApartmentId,id)
                .remove();
        //5.删除公寓杂费关系列表
        apartmentFeeValueService.lambdaUpdate()
                .eq(ApartmentFeeValue::getApartmentId,id)
                .remove();
        return Result.ok("删除公寓信息成功");
    }
}




